home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / stack / stkrst.c < prev    next >
C/C++ Source or Header  |  1995-05-23  |  2KB  |  61 lines

  1. #include <bases.h>
  2.  
  3. asm("
  4. | the stack consists of a single linked linear list like this:
  5. |
  6. | struct stack
  7. | { struct stack *next;            points to the next underlying structure
  8. |   struct StackSwapStruct sss;        holds previous stackframe when used,
  9. |                    itself when unused, the current stackborders
  10. |                    are in the task structure ;-)
  11. |   APTR top_of_stackframe; }        previous variable value
  12. |                    Note: stack->sss.stk_Lower is not reliable
  13. |
  14. | For better performance stackframes are cached in a 'unused' list when
  15. | not in use. (AllocMem() is really a performance killer).
  16. |
  17.  
  18. |
  19. | Caution:
  20. | Race condition ahead! exec might preempt our task at any time storing
  21. | the current register set on top of the free stack. NEVER set a sp higher
  22. | than the location of important data.
  23. |
  24.     .comm    ___used_stack,8        | pointer to used stackframes,
  25.                     | pointer to unused stackframes
  26.     .text
  27.     .even
  28.     .globl    ___stkrst
  29.  
  30. ___stkrst:
  31.     movel    a2,sp@-
  32.     moveml    #0x40f2,sp@-
  33.     movel    sp,a2
  34.     movel    d0,a3
  35. l0:    lea    "A4(___used_stack)",a1
  36.     tstl    a1@
  37.     beq    l1            | No previous stackframe
  38.     movel    "A4(___stackborders)",a0
  39.     cmpl    a0@,a3
  40.     jcs    l2
  41.     cmpl    a0@(4:W),a3
  42.     jhi    l2            | Stackpointer points to current frame
  43. l1:    movel    a2@(28:W),a3@-        | Store returnaddress
  44.     movel    a3,d0
  45.     moveml    a2@+,#0x4f02        | Restore registers
  46.     movel    a2@,a2
  47.     movel    d0,sp            | Set stackpointer AFTER restoring registers!
  48.     addql    #4,d0            | This function may return on the same
  49.     rts                | stackframe it was called on.
  50. l2:    movel    a1@,a0            | Go to previous stack
  51.     movel    a0@,a1@+
  52.     movel    a1@,a0@
  53.     movel    a0,a1@
  54.     addqw    #4,a0
  55.     movel    a0@(12:W),"A4(___stk_limit)"
  56.     movel    "A4(_SysBase)",a6
  57.     jsr    a6@(-0x2dc)        | StackSwap(sss:a0)
  58.     subqw    #4,sp            | Avoid overwriting returnaddress
  59.     jra    l0
  60. ");
  61.